//
//  SeekoBoardTableViewController.m
//  Moko
//
//  Created by cj848 on 11. 7. 25..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SeekoBoardTableViewController.h"
#import "HTMLNode.h"
#import "MokoAppDelegate.h"
#import "SeekoBoardListTableViewController.h"
#import "GradientCell.h"
@implementation SeekoBoardTableViewController
@synthesize menuVO;
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil menuVO:(MenuVO*)vo {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if( self ) {
        cellColorArray = [[NSArray alloc] initWithObjects:[NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0].CGColor, nil], [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)(id)RGBA(199, 214, 238, 1.0).CGColor, nil], nil];
        self.menuVO = vo;
        self.title = menuVO.title;
    }
    return self;
}
-(void)setRightButton {
    GET_APPDELEGATE;
    UIImage* image = nil;
    if( [delegate.httpClient didLogin] ) {
        image = [UIImage imageNamed:@"logout_on.png"];
    } else {
        image = [UIImage imageNamed:@"login_on.png"];
    }
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightButton) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, image.size.width*0.75, image.size.height*0.75);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
}

- (void)dealloc
{
    [menuVO release];
    [cellColorArray release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setRightButton];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setRightButton];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [menuVO.childVOArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    GradientCell *cell = (GradientCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[GradientCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.opaque = NO;
        cell.textLabel.highlightedTextColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:23];
    }
    CAGradientLayer* gradientLayer = (CAGradientLayer*)cell.backgroundView.layer;
    gradientLayer.colors = [cellColorArray objectAtIndex:(indexPath.row & 1)];
    //cell.textLabel.text = [menuVO.childVOArr objectAtIndex:indexPath.row];
    cell.textLabel.text = ((MenuVO*)[menuVO.childVOArr objectAtIndex:indexPath.row]).title;
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
        return 44;
    }
    else
        return 60;
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MenuVO* childVO = [menuVO.childVOArr objectAtIndex:indexPath.row];
    GET_APPDELEGATE;
    // 푸쉬한 뒤에 릴리즈 혹은 오토 릴리즈 해버리면 좆망됨 CALayer release message sent to deallocated instance 에러 뜸... 푸쉬하고 팝하면 이미 릴리즈 된 상태
    SeekoBoardListTableViewController* listView = [[SeekoBoardListTableViewController alloc] initWithNibName:@"SeekoBoardListTableViewController" bundle:[NSBundle mainBundle] title:childVO.title link:[NSString stringWithFormat:@"%@%@", delegate.configVO.siteHostAddress,childVO.link]];
    [listView becomeFirstResponder];
    [self.navigationController pushViewController:listView animated:YES];
}
-(void) rightButton {
    GET_APPDELEGATE;
    // 로그인 상태일 경우 로그아웃을 수행한다.
    if( [delegate.httpClient didLogin] ) {
        [delegate.httpClient requestURL:[NSString stringWithFormat:@"%@/zboard4/logout.php", delegate.configVO.siteHostAddress] httpMethod:@"GET" bodyData:nil isMultiPart:NO boundary:nil target:self selector:@selector(finishLoading:)];
        
    } else {
        [self presentModalViewController:delegate.loginViewController animated:YES];
    }
}

-(void)finishLoading:(NSData*) data {
    GET_APPDELEGATE;
    delegate.httpClient.isLogin = NO;
    [self presentModalViewController:delegate.loginViewController animated:YES];
}
@end
